﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace XF.FM.Simple.Controllers
{
    public class OptionTestController : Controller
    {
        IXfService _xfService;
        public OptionTestController(IXfService xfService)
        {
            _xfService = xfService;
        }
        public IActionResult Index()
        {
            return View(_xfService.GetOptions().Where(m => m.Key != "nav_admin").ToList());
        }
        [HttpPost]
        public IActionResult Reload(string key)
        {
            var r = _xfService.GetOption(key).Reload();
            if (string.IsNullOrEmpty(r))
                return Json(new { ret = 0 });
            else
                return Json(new { ret = 1, Msg = r });
        }
        [HttpPost]
        public IActionResult Reset(string key)
        {
            _xfService.GetOption(key).ReSet(TestMenuSource.GetModel());
            return Json(new { ret = 0 });
        }
        [HttpGet]
        public IActionResult Edit(string key)
        {
            return Json(new { Ret = 0, Msg = System.Text.Json.JsonSerializer.Serialize(_xfService.GetOption(key).Value,XfConfig.DefaultJsonSerializerOptions) });
        }
        public IActionResult GetPath()
        {
            string path = Path.Combine("xfconfig", "nav_test.json");
            string r = "";
            bool hasfile = System.IO.File.Exists(path);
            if (hasfile)
            {
                r = System.IO.File.ReadAllText(path);
            }
            return Content(hasfile + r);
        }
    }
}